package com.dalin.platform.loa.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.dalin.common.base.BaseObject;
import com.dalin.platform.pub.PlatContext;
/*****************************************************************************
 * @content: 用户类信息                                                                                                                           *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-05-11                                                     *
 * @modDate: 2010-05-11                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
@Entity
@Table(name="LOA_USER")
public class LoaUserEntity extends BaseObject {
	private Long userId; // USER_ID,16,0
    private Long projectId; // PROJECT_ID,16,0
    private Long organId; // ORGAN_ID,16,0
    private String realName;
    private String loginName; // LOGIN_NAME,64,0
    private String loginPswd; // LOGIN_PSWD,64,0
    private Integer sex;
    private String remark; // REMARK,256,0
    private Integer defaultDataLevel;
    private Date createDate; // CREATE_DATE,7,0
    private Date modifyDate; // MODIFY_DATE,7,0
    private Integer flagDel; // 

    private Set<LoaRoleEntity> loaRoleEntitys = new HashSet<LoaRoleEntity>(0);




    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy = GenerationType.TABLE,generator="generator_normal")
    @TableGenerator(
        name = "generator_normal",
        table = "GLB_GENERATOR_KEY",
        pkColumnName = "GEN_KEY",
        valueColumnName = "GEN_VALUE",
        pkColumnValue = "LOA_USER",
        allocationSize=1
    )
    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Column(name="PROJECT_ID")
    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }

    @Column(name="ORGAN_ID")
    public Long getOrganId() {
        return organId;
    }

    public void setOrganId(Long organId) {
        this.organId = organId;
    }
	@Column(name="REAL_NAME")
	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	@Column(name="SEX")
	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}


    @Column(name="LOGIN_NAME")
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @Column(name="LOGIN_PSWD")
    public String getLoginPswd() {
        return loginPswd;
    }

    public void setLoginPswd(String loginPswd) {
        this.loginPswd = loginPswd;
    }

    @Column(name="REMARK")
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Column(name="DEFAULT_DATA_LEVEL")
    public Integer getDefaultDataLevel() {
		return defaultDataLevel;
	}

	public void setDefaultDataLevel(Integer defaultDataLevel) {
		this.defaultDataLevel = defaultDataLevel;
	}

	@Temporal(TemporalType.TIMESTAMP)
    @Column(name="CREATE_DATE")
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="MODIFY_DATE")
    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Column(name="FLAG_DEL")
    public Integer getFlagDel() {
		return flagDel;
	}

	public void setFlagDel(Integer flagDel) {
		this.flagDel = flagDel;
	}
	
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinTable(name = "LOA_ROLE_USER", joinColumns = {
	        @JoinColumn(name = "USER_ID")
	    }, inverseJoinColumns =  {
	        @JoinColumn(name = "ROLE_ID")
	    }
    )
    public Set<LoaRoleEntity> getLoaRoleEntitys() {
		return loaRoleEntitys;
	}

	public void setLoaRoleEntitys(Set<LoaRoleEntity> loaRoleEntitys) {
		this.loaRoleEntitys = loaRoleEntitys;
	}
	
	
	
	
	
	
	
	
	

	@Transient
    public LoaOrganizationEntity getLoaOrganizationEntity() throws Exception {
		return (LoaOrganizationEntity) PlatContext.getDao()
			.findObjById(LoaOrganizationEntity.class, this.getOrganId());
	}
	
	
	@Transient
	public Set<LoaMenuEntity> getLoaUserMenuEntitys() {
		Set<LoaMenuEntity> menuSet = new HashSet<LoaMenuEntity>();
		for(LoaRoleEntity role : this.getLoaRoleEntitys()) {
			Set<LoaMenuEntity> menus = role.getLoaMenuEntitys();
			for(LoaMenuEntity menu : menus) {
				menuSet.add(menu);
			}
		}
		return menuSet;
	}
	
	
    @Override
    public int hashCode() {
        int njlrst = 17;
        njlrst = 31 * njlrst + (userId == null ? 0 : userId.hashCode());
        njlrst = 31 * njlrst + (projectId == null ? 0 : projectId.hashCode());
        njlrst = 31 * njlrst + (organId == null ? 0 : organId.hashCode());
        njlrst = 31 * njlrst + (loginName == null ? 0 : loginName.hashCode());
        njlrst = 31 * njlrst + (loginPswd == null ? 0 : loginPswd.hashCode());
        njlrst = 31 * njlrst + (remark == null ? 0 : remark.hashCode());
        njlrst = 31 * njlrst + (createDate == null ? 0 : createDate.hashCode());
        njlrst = 31 * njlrst + (modifyDate == null ? 0 : modifyDate.hashCode());
        njlrst = 31 * njlrst + (loaRoleEntitys == null ? 0 : loaRoleEntitys.hashCode());
        return njlrst;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LoaUserEntity)) return false;
        final LoaUserEntity other = (LoaUserEntity) o;
        if (userId == null) {
            if (other.userId != null) return false;
        } else if (!userId.equals(other.userId)) {
            return false;
        }
        if (projectId == null) {
            if (other.projectId != null) return false;
        } else if (!projectId.equals(other.projectId)) {
            return false;
        }
        if (organId == null) {
            if (other.organId != null) return false;
        } else if (!organId.equals(other.organId)) {
            return false;
        }
        if (loginName == null) {
            if (other.loginName != null) return false;
        } else if (!loginName.equals(other.loginName)) {
            return false;
        }
        if (loginPswd == null) {
            if (other.loginPswd != null) return false;
        } else if (!loginPswd.equals(other.loginPswd)) {
            return false;
        }
        if (remark == null) {
            if (other.remark != null) return false;
        } else if (!remark.equals(other.remark)) {
            return false;
        }
        if (createDate == null) {
            if (other.createDate != null) return false;
        } else if (!createDate.equals(other.createDate)) {
            return false;
        }
        if (modifyDate == null) {
            if (other.modifyDate != null) return false;
        } else if (!modifyDate.equals(other.modifyDate)) {
            return false;
        }
        if (loaRoleEntitys == null) {
            if (other.loaRoleEntitys != null) return false;
        } else if (!loaRoleEntitys.equals(other.loaRoleEntitys)) {
            return false;
        }
        return true;
    }
}
